<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Home\Controller\SourceController;
class PointController extends CommonController {
	public function _initialize(){

		parent::_initialize();
	}
	/**
	 * 点位首页
	 */
	public function index(){
		$data['point_config']=M('Point_config')->select();
		$data['currency_list']=M('Currency')->where('is_line = 1')->select();
		$data['msg']=M('Message_all')->order('add_time')->find();
		$this->ajaxReturn($data,'JSONP');
		
	}
	/**
	 * 获取当前价格
	 */
	public function getPrice(){
		$SOURCE=new SourceController();
		$currency_id=empty(I('currency_id'))?'25':I('currency_id');
		$result=$SOURCE->result($currency_id);
		$lever=M('Currency')->where('currency_id ='.$currency_id)->find()['lever'];
		$ran=$this->random();
		$data['High']=$result->High*$lever;
		$data['Low']=$result->Low*$lever;
		$data['Open']=$result->Open*$lever;
		$data['Close']=$result->LastClose*$lever;
		$data['now_price']=$result->Price*$lever+$ran;
		$this->ajaxReturn($data,'JSONP');
	}
	/**
	 * 生成随机数
	 */
	private function random(){
		$time=time();
		$timen=substr($time, -2);
		$minute=date('i',$time);
		$second=date('s',$time);
		$num=round(($timen+$minute+$second)/300,2);
		if($timen%2==0){
			$num=0-$num;
		}
		return $num;
	}
	/**
	 * 添加点位订单
	 */
	public function addPoint(){
		$a=$this->checkTime(I('currency_id'));
		if(!$a){
			$arr['status']=0;
			$arr['info']='交易未开启，请在交易时间内进行交易';
			$this->ajaxReturn($arr,'JSONP');
		}
// 		if(empty($_SESSION['is_pwd'])){
// 			$arr['status']=0;
// 			$arr['info']='交易密码过期，请重新输入';
// 			$this->ajaxReturn($arr,'JSONP');
// 		}
		$member=M('Member')->where('member_id ='.$_SESSION['member_id'])->find();
		if($member['rmb']<I('money')){
			$arr['status']=0;
			$arr['info']='账户余额不足';
			$this->ajaxReturn($arr,'JSONP');
		}
		$data['order_num']=$_SESSION['member_id'].time();
		$data['member_id']=$_SESSION['member_id'];
		$data['currency_id']=I('currency_id');
		$data['buy_point']=I('buy_point');
		$data['money']=I('money');
		$data['point_type']=I('point_type'); 
		$data['type']=I('type');
		$data['add_time']=time();
		$data['status']=0;
		if(I('type')==1){
			$point=M('Point_config')->where('id= '.I('point_type'))->find()['point'];
			$data['out_point']=I('buy_point')+$point;
		}
		if(I('type')==2){
			$point=M('Point_config')->where('id= '.I('point_type'))->find()['point'];
			$data['out_point']=I('buy_point')-$point;
		}
		
		$r[]=M('Point_orders')->add($data);
		$r[]=M('Member')->where('member_id ='.$_SESSION['member_id'])->setDec('rmb',I('money'));
		if(in_array(false, $r)){
			$arr['status']=0;
			$arr['info']='操作失败';
			$this->ajaxReturn($arr,'JSONP');
		}else{
			$arr['status']=1;
			$arr['info']='操作成功';
			$this->ajaxReturn($arr,'JSONP');
		}
	}
	
	/**
	 * 点位记录查询
	 */
	public function record(){
		$SOURCE=new SourceController();
		$rmb=M('Member')->where('member_id ='.$_SESSION['member_id'])->find()['rmb'];
		$point_money=M('Point_orders')->where('status = 0 and member_id ='.$_SESSION['member_id'])->sum('money');
		$where1['member_id']=$_SESSION['member_id'];
		$where1['add_time']=['BETWEEN',[strtotime(date( "Y-m-d", time())),time()]];
		$point_count=M('Point_orders')->where($where1)->count();
		$data['rmb']=$rmb;
		$data['point_money']=$point_money;
		$data['point_count']=$point_count;
		if(!empty(I('type'))){
			$where['type']=I('type');
		}
		if(!empty(I('status'))){
			$where['status']=I('status');
		}
		$where['member_id']=$_SESSION['member_id'];
		$list=M('Point_orders')->where($where)->select();
		if(!$list){
			$data['list']=$list;
			$data['status']=0;
			$this->ajaxReturn($data,'JSONP');
		}else{
			foreach ($list as $k=>$v){
				$currency=M('Currency')->where('currency_id ='.$v['currency_id'])->find();
				$list[$k]['currency_name']=$currency['currency_name'];
				$point_config=M('Point_config')->where('id = '.$v['point_type'])->find();
				$list[$k]['point_num']=$point_config['point'];
				$result=$SOURCE->result($v['currency_id']);
				$now_price=$result->Price;
				$list[$k]['now_price']=$now_price*$currency['lever']; 
			}
			$data['list']=$list;
			$data['status']=1;
			$this->ajaxReturn($data,'JSONP');
		}
	}
	/**
	 * 检测交易时间
	 * @param unknown $currency_id
	 * @return boolean
	 */
	private function checkTime($currency_id){
		$currency=M('Currency')->where('currency_id ='.$currency_id)->find();
		$time=strtotime(date('Y-m-d'));
		$start_time=$time+$currency['start_time_h']*3600+$currency['start_time_m']*60;
		$over_time=$time+$currency['end_time_h']*3600+$currency['end_time_m']*60;
		$start_x_time=$time+$currency['start_time_x_h']*3600+$currency['start_time_x_m']*60;
		$over_x_time=$time+$currency['end_time_x_h']*3600+$currency['end_time_x_m']*60;
		if((time()<$start_time||time()>$over_time)  &&  (time()<$start_x_time||time()>$over_x_time)){
			return false;
		}
		$week = date("l",time());  //获取当前日期是周几
		if($week == 'Saturday' && $currency['is_lock_6'] == 1){
			return false;
		}
		if($week == 'Sunday' && $currency['is_lock_7'] == 1 ){
			return false;
		}
		return true;
	}
}